<template>
  <header>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <!--   container 使得导航栏布局变好看  -->
      <div class="container">
        <a class="navbar-brand fa fa-video-camera" href="#">&nbsp;乾哥课程</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <router-link class="nav-link" to="/">
                主页
              </router-link>
            </li>
            <li class="nav-item active">
              <router-link class="nav-link" to="/list">
                全部课程
              </router-link>
            </li>
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-expanded="false">
                更多
              </a>
              <div class="dropdown-menu">
                <a class="dropdown-item" href="#">关于我们</a>
                <a class="dropdown-item" href="#">渠道合作</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="#">更多信息</a>
              </div>
            </li>
          </ul>
          <span class="text-white pr-3">您好：{{loginMember.name}}</span>
          <button v-show="loginMember.id" @click="logout()" class="btn btn-outline-light my-2 my-sm-0">退出登录</button>
          <button v-show="!loginMember.id" @click="openRegisterMember" class="btn btn-outline-success my-2 my-sm-0" type="submit">登录/注册</button>
        </div>
      </div>
    </nav>
    <login ref="loginModal" @setLoginMember="setLoginMember"></login>
  </header>
</template>

<script>
import Login from "@/views/login";
import {onMounted, ref} from "vue";
import axios from "axios";
export default {
  name: "the-header",
  components: {Login},
  setup() {
    const loginModal = ref();
    const loginMember = ref({}); // 登录会员

    /**
     *打开 注册登录模态框
     */
    const openRegisterMember = () => {
      loginModal.value.openRegisterMember();
    }

    /**
     * 登录成功后 login组件调用
     */
    const setLoginMember = ()=>  {
      loginMember.value = Tool.getLoginMember();
    }

    /**
     * 退出登录
     */
    const logout = () => {
      // 清除前端 后端缓存
      axios.get(process.env.VUE_APP_SERVER + '/business/web/member/logout/' + loginMember.value.token).then((response)=>{
        let resp = response.data;
        if (resp.success) {
          Tool.setLoginMember(null);
          loginMember.value = {};
          Toast.success("退出登录成功");
        } else {
          Toast.warning(resp.message);
        }
      });
    }

    onMounted(() => {
      loginMember.value = Tool.getLoginMember();
    })

    return {
      openRegisterMember,
      loginModal,
      loginMember,
      setLoginMember,
      logout,
    }
  }
}
</script>

<style scoped>

</style>